package view;

import common.Util;
import dao.Blog;
import dao.BlogDao;
import dao.User;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.WebContext;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebListener;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 刘柄华
 * Date: 2022-06-08
 * Time: 14:18
 */
// WebServlet 所关联的路径, 里面是可以包含 . 的
// 后面去浏览器中访问的 blog_list.html 就不再是静态的页面了, 而是动态生成的了.
@WebServlet("/blog_list.html")
public class BlogListServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html; charset=utf-8");

        // 针对登录状态的监测, 未登录时则重定向到登录页面
        User user = Util.checkLoginStatus(req);
        if(user == null){
            System.out.println("当前用户未登录");
            resp.sendRedirect("login.html");
            return;
        }

        // 1. 从数据库中拿到所有的博客列表
        BlogDao blogDao = new BlogDao();
        List<Blog> blogs = blogDao.selectAll();

        // 根据 UserId 在 blog 表中查出所属该用户的博客数目
        int blogNum = blogDao.selectNum(user.getUserId());

        // 2.通过模板引擎渲染页面
        ServletContext context = this.getServletContext();
        TemplateEngine engine = (TemplateEngine) context.getAttribute("engine");
        WebContext webContext = new WebContext(req, resp, context);
        webContext.setVariable("blogNum",blogNum);
        webContext.setVariable("user",user);
        webContext.setVariable("blogs",blogs);
        String html = engine.process("blog_list", webContext);
        resp.getWriter().write(html);
    }
}
